/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.admin.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.MoreTable;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.ConditionImpl;
import org.teasoft.honey.osql.core.BeeFactoryHelper;
import org.teasoft.honey.osql.core.Logger;
import org.teasoft.honey.util.ObjectUtils;
import org.teasoft.honey.util.StringUtils;

import com.aiteasoft.school.admin.entity.Assignclass;
import com.aiteasoft.school.admin.entity.Assigncourse;
import com.aiteasoft.school.admin.entity.Assignexam;
import com.aiteasoft.school.admin.service.AssignexamService;
import com.aiteasoft.school.scores.entity.Scores;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-21 10:42:28
 */
@Service
public class AssignexamServiceImpl implements AssignexamService {

	@Autowired
	SuidRich suidRich;

	@Override
	public Assignexam process(Assignexam entity) { 
		//Add the business logic if need. 放一些公共设置

		return entity;
	}
	
	@Override
	public boolean isWrongBizForUpdate(Result result, Assignexam entity) {
		//Add the business logic if need.

		return false;
	}

	public PageWarp countAndSelect(Assignexam entity, int start, int size) {
	    Condition condition = getCondition(entity);
	    
		int total = suidRich.count(entity, condition);
		
		condition.start(start);
		condition.size(size);
		List<Assignexam> listPage=suidRich.select(entity, condition);
		
		return new PageWarp(total,listPage);
	}
	
	private Condition getCondition(Assignexam entity) {
		Condition condition=new ConditionImpl();
		//Add the business logic if need.
		
		return condition;
	}
	
	@Override
	public int initExam(String classno) {
		
//		CurrentCoureseTeacherView view=new CurrentCoureseTeacherView();
//		if(StringUtils.isNotBlank(classno)) view.setClassno(classno); //可以只设置一个班.
//		List<CurrentCoureseTeacherView> list=suidRich.select(view);
		
		MoreTable moreTable = BeeFactoryHelper.getMoreTable();
		Assigncourse assigncourse=new Assigncourse();
		assigncourse.setStatus("1");
		Assignexam assignexam=new Assignexam(); //主表
		assignexam.setStatus("1");
		if(StringUtils.isNotBlank(classno)) assignexam.setClassno(classno); //可以只设置一个班.
		assignexam.setAssigncourse(assigncourse);
		
		List<Assignexam> list =moreTable.select(assignexam);
		
		if(ObjectUtils.isEmpty(list)) return -2;
		
		List<Assignclass> listStu;
		Assignclass assignclass=new Assignclass();
		List<Scores> listScores;
		Assignexam tempView;
		Scores scores;
		int insertTotal=0;
		for (int i = 0;  list!=null && i < list.size(); i++) {
			tempView=list.get(i);
			assignclass.setClassno(tempView.getClassno());
			assignclass.setStatus("1"); //只查当前有效的数据,若重新分班会有历史数据.
			listStu=suidRich.select(assignclass);
			listScores=new ArrayList<>();
			for (int j = 0;listStu!=null && j < listStu.size(); j++) {
				scores=new Scores();
				setFieldFromView(scores,tempView);
				setStudentInfo(scores, listStu.get(j));
				scores.setCreater("SYS");
				scores.setStatus("INIT");
				listScores.add(scores);
			}
			int insertNum=suidRich.insert(listScores,"createtime,updatetime");  //一次插入一个班的数量
			insertTotal+=insertNum;
			if (insertNum == listScores.size())
				Logger.info("已成功插入记录数：" + insertNum);
			else
				Logger.info("已成功插入记录数：" + insertNum + "  未成功插入记录数: " + (listScores.size() - insertNum));
		
			Assignexam assignexamUpdate=new Assignexam();
			assignexamUpdate.setId(tempView.getId());
			assignexamUpdate.setStatus("2");  //Assignexam 状态设为2.表示该考试已初始化成绩
			suidRich.update(assignexamUpdate);
			
		}// for i
		
		return insertTotal;
	}
	
	private void setFieldFromView(Scores scores,Assignexam assignexam){
		scores.setClassno(assignexam.getClassno());
		scores.setTerm(assignexam.getTerm());
		scores.setExamno(assignexam.getExamno());
		scores.setSubject(assignexam.getSubject());
		scores.setSubjectno(assignexam.getSubjectno());
		scores.setTeachername(assignexam.getAssigncourse().getName());
		scores.setTeacherno(assignexam.getAssigncourse().getTeacherno());
		scores.setSchoolno(assignexam.getSchoolno());
		
//		分数表涉及三部分数据:					
//			1.从考试安排表中获取					
//			班级编号	学期	考试编号	课程号	课程名称	
//			2.从班级表获取学生信息					
//			3.从排课表中获取班级与老师与课程信息					
//			班级编号	学期	老师编号	老师姓名	课程号	课程名称

	}
	
	private void setStudentInfo(Scores scores,Assignclass assignclass){
		scores.setStuno(assignclass.getStuno());
		scores.setName(assignclass.getName());
		scores.setScore(0D); //0
	}
	

}